package com.learn.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.learn.resp.Result;
import com.learn.resp.ReturnCodeEnum;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author DJJ_F
 * @version 1.0
 * @date 2025/08/05
 * @description ch11 nacos 服务端9001
 * 整合openfeign
 */
@RestController
public class PayController {

    @Value("${server.port}")
    private String port;

    @GetMapping("/pay/get/{id}")
    public Result<String> get(@PathVariable("id") Integer id) {
        return Result.success("get id: " +id + ", get port: " + port);
    }

    /**
     * sentinel 整合 openfeign
     * blockHandler 需要自己写
     * @param id
     * @return
     */
    @GetMapping("/pay/get/sentinel/{id}")
    @SentinelResource(value = "getSentinel", blockHandler = "getSentinelBlockHandler")
    public Result<String> getSentinel(@PathVariable("id") Integer id) {
        return Result.success("get sentinel id: " +id + ", get port: " + port);
    }

    public Result<String> getSentinelBlockHandler(Integer id, BlockException blockException) {
        return Result.fail(ReturnCodeEnum.RC500.getCode(), "违反sentinel流控配置规则");
    }
}
